package com.hankol.winecellar.utils

import android.view.Gravity
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.google.android.flexbox.FlexboxLayout
import android.util.TypedValue
import com.hankol.winecellar.R
import com.hankol.winecellar.bean.LabelConfigFacilityBean
import com.xinlan.imageeditlibrary.editimage.utils.DensityUtil


object FlexBoxUtils {

    fun addChildToFlexboxLayout4Mall(flexboxLayout: FlexboxLayout, item: String) {
        val params = FlexboxLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
        params.setMargins(0, 0, DensityUtil.dip2px(flexboxLayout.context, 4f), 0)
        val tv = TextView(flexboxLayout.context)
        tv.text = item
        tv.gravity = Gravity.CENTER
        tv.textSize = 10f
        tv.setBackgroundResource(R.drawable.label_f54d53_a10_r4)
        tv.layoutParams = params
        tv.setTextColor(ContextCompat.getColor(tv.context, R.color.color_D53))
        flexboxLayout.addView(tv)
    }

    fun addChildToFlexboxLayout(flexboxLayout: FlexboxLayout, item: LabelConfigFacilityBean, isHorizontal: Boolean = false) {
        val params = FlexboxLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
        params.setMargins(0, 0, DensityUtil.dip2px(flexboxLayout.context, 6f), DensityUtil.dip2px(flexboxLayout.context, 3f))
        val tv = TextView(flexboxLayout.context)
        if (isHorizontal) {
            tv.text = if (item.labelName.length > 6) "${
                item.labelName.substring(0, 5)
            }..." else item.labelName
        } else {
            tv.text = item.labelName
        }
        tv.gravity = Gravity.CENTER
        tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10f )
        tv.setBackgroundResource(R.drawable.label_d8_r2)
        tv.layoutParams = params
        tv.tag = item.id
        tv.setTextColor(ContextCompat.getColor(tv.context, R.color.color_333333))
        flexboxLayout.addView(tv)
    }

    //酒窖服务 弹窗里的标签
    fun addChildToFlexboxLayout4Pop(flexboxLayout: FlexboxLayout, item: LabelConfigFacilityBean) {
        val params = FlexboxLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
        params.setMargins(DensityUtil.dip2px(flexboxLayout.context, 3f), 0, DensityUtil.dip2px(flexboxLayout.context, 3f), DensityUtil.dip2px(flexboxLayout.context, 6f))
        val tv = TextView(flexboxLayout.context)
        tv.text = item.labelName
        tv.gravity = Gravity.CENTER
        tv.textSize = 14f
        tv.setBackgroundResource(R.drawable.label_r8)
        tv.layoutParams = params
        tv.tag = item.id
        tv.setTextColor(ContextCompat.getColor(tv.context, R.color.color_333333))
        flexboxLayout.addView(tv)
    }

    //没有背景的标签
    fun addChildToFlexboxLayoutNoBg(flexboxLayout: FlexboxLayout, item: LabelConfigFacilityBean) {
        val params = FlexboxLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
        params.setMargins(DensityUtil.dip2px(flexboxLayout.context, 17f), 0, DensityUtil.dip2px(flexboxLayout.context, 17f), DensityUtil.dip2px(flexboxLayout.context, 5f))
        val tv = TextView(flexboxLayout.context)
        tv.text = item.labelName
        tv.gravity = Gravity.CENTER
        tv.textSize = 14f
        tv.layoutParams = params
        tv.tag = item.id
        tv.setTextColor(ContextCompat.getColor(tv.context, R.color.color_333333))
        flexboxLayout.addView(tv)
    }

    /* ：存储点击的标签*/
    var tagSelectList1 = mutableListOf<LabelConfigFacilityBean>()//特色
    var tagSelectList2 = mutableListOf<LabelConfigFacilityBean>()//基础

    //酒窖详情  标签
    fun addChildToFlexboxLayout(tagList: List<LabelConfigFacilityBean>, isMoreSelect: Boolean, flexboxLayout: FlexboxLayout, item: LabelConfigFacilityBean, tag: Int) {
        val params = FlexboxLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
        params.setMargins(DensityUtil.dip2px(flexboxLayout.context, 5f), 0, DensityUtil.dip2px(flexboxLayout.context, 5f), DensityUtil.dip2px(flexboxLayout.context, 11f))
        val tv = TextView(flexboxLayout.context)
        tv.text = item.labelName
        tv.gravity = Gravity.CENTER
        tv.textSize = 14f
        tv.setBackgroundResource(R.drawable.bg_solid_f9f9f9_r6)
        tv.layoutParams = params
        tv.tag = item
        tv.setTextColor(ContextCompat.getColor(tv.context, if (item.checked) R.color.color_D53 else R.color.color_333333))
        tv.isSelected = item.checked
        tv.setOnClickListener {
            if (isMoreSelect) {
                //多选
                item.checked = !item.checked
            }
            flexboxLayout.removeAllViews()
            tagList.forEach {
                addChildToFlexboxLayout(tagList, isMoreSelect, flexboxLayout, it, tag)
            }
            if (item.checked) {
                if (tag == 1) {
                    tagSelectList1.add(LabelConfigFacilityBean(item.labelName, item.id))
                } else {
                    tagSelectList2.add(LabelConfigFacilityBean(item.labelName, item.id))
                }
            } else {
                val iterator = (if (tag == 1) tagSelectList1 else tagSelectList2).iterator()
                while (iterator.hasNext()) {
                    if (iterator.next().id == item.id) {
                        iterator.remove()
                    }
                }
            }
        }
        flexboxLayout.addView(tv);
    }


    fun addChildToFlexboxLayout(flexboxLayout: FlexboxLayout, item: String, isHorizontal: Boolean = false) {
        val params = FlexboxLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
        params.setMargins(0, 0, DensityUtil.dip2px(flexboxLayout.context, 6f), DensityUtil.dip2px(flexboxLayout.context, 3f))
        val tv = TextView(flexboxLayout.context)
        if (isHorizontal) {
            tv.text = if (item.length > 6) "${
                item.substring(0, 5)
            }..." else item
        } else {
            tv.text = item
        }
        tv.gravity = Gravity.CENTER
        tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10f )
        tv.setBackgroundResource(R.drawable.label_f4f4f4_r2)
        tv.layoutParams = params
        tv.tag = item
        tv.setTextColor(ContextCompat.getColor(tv.context, R.color.color_333333))
        flexboxLayout.addView(tv)
    }
    fun addChildToFlexboxLayoutD8(flexboxLayout: FlexboxLayout, item: String, isHorizontal: Boolean = false) {
        val params = FlexboxLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
        params.setMargins(0, 0, DensityUtil.dip2px(flexboxLayout.context, 6f), DensityUtil.dip2px(flexboxLayout.context, 3f))
        val tv = TextView(flexboxLayout.context)
        if (isHorizontal) {
            tv.text = if (item.length > 6) "${
                item.substring(0, 5)
            }..." else item
        } else {
            tv.text = item
        }
        tv.gravity = Gravity.CENTER
        tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10f )
        tv.setBackgroundResource(R.drawable.label_d8_r2)
        tv.layoutParams = params
        tv.tag = item
        tv.setTextColor(ContextCompat.getColor(tv.context, R.color.color_333333))
        flexboxLayout.addView(tv)
    }
}
